package com.beikai.mybatisplus.user.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.beikai.mybatisplus.common.units.JsonResult;
import com.beikai.mybatisplus.user.model.UserModel;
import com.beikai.mybatisplus.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author beikai
 * @since 2019-06-15
 */
@RestController
@RequestMapping("/user/user-model")
public class UserController {

    @Autowired
    private IUserService userService;


    @ApiOperation("获取用户信息-方式一")
    @PostMapping("/getAllUser1")
    public JSONObject getAllUser1() {
        return JsonResult.resultSuccess(userService.getAllUser1());
    }

    @ApiOperation("获取用户信息-方式二")
    @PostMapping("/getAllUser2")
    public JSONObject getAllUser2() {
        return JsonResult.resultSuccess(userService.getAllUser2());
    }

    @ApiOperation("获取用户信息-方式三")
    @PostMapping("/getAllUser3")
    public JSONObject getAllUser3() {
        return JsonResult.resultSuccess(userService.getAllUser3());
    }

    @ApiOperation("获取用户信息通过主键-方式一")
    @PostMapping("/getAllUserById1")
    public JSONObject getAllUserById1(
            Long id
    ) {
        return JsonResult.resultSuccess(userService.getAllUserById1(id));
    }

    @ApiOperation("获取用户信息通过主键-方式二")
    @PostMapping("/getAllUserById2")
    public JSONObject getAllUserById2(
            Long id
    ) {
        return JsonResult.resultSuccess(userService.getAllUserById2(id));
    }

    @ApiOperation("获取用户信息通过主键-方式三")
    @PostMapping("/getAllUserById3")
    public JSONObject getAllUserById4(
            Long id
    ) {
        return JsonResult.resultSuccess(userService.getAllUserById3(id));
    }

    @ApiOperation("添加用户信息")
    @PostMapping(value = "/addUser1", produces = "application/json")
    public JSONObject addUser1(
            @RequestBody UserModel userModel
    ) {
        return JsonResult.resultSuccess(userService.add(userModel));
    }

    /**
     * restful 风格
     *
     * @param name
     * @param email
     * @param age
     * @return
     */
    @ApiOperation("添加用户信息")
    @PostMapping("/addUser2/{name}/{email}/{age}")
    public JSONObject addUser2(
            String name,
            String email,
            Integer age
    ) {
        return JsonResult.resultSuccess(userService.add(name, age, email));
    }

    @ApiOperation("批量添加用户信息")
    @PostMapping("/addBatchUser")
    public JSONObject addUser2(
            @RequestBody List<UserModel> userModels
    ) {
        return JsonResult.resultSuccess(userService.addBatchUser(userModels));
    }

    @ApiOperation("批量添加用户信息 指定插入批次数量")
    @PostMapping("/addBatchUserByNum")
    public JSONObject addBatchUserByNum(
            @RequestBody List<UserModel> userModels,
            Integer num
    ) {
        return JsonResult.resultSuccess(userService.addBatchUserByNum(userModels, num));
    }

    @ApiOperation("添加或修改用户信息")
    @PostMapping("/addUserOrUpdate")
    public JSONObject addUserOrUpdate(
            @RequestBody UserModel userModels
    ) {
        return JsonResult.resultSuccess(userService.addUserOrUpdate(userModels));
    }


    @ApiOperation("批量添加或修改用户信息")
    @PostMapping("/addBatchUserOrUpdate")
    public JSONObject addBatchUserOrUpdate(
            @RequestBody List<UserModel> userModels
    ) {
        return JsonResult.resultSuccess(userService.addBatchUserOrUpdate(userModels));
    }

    @ApiOperation("批量添加或修改用户信息 指定插入批次数量")
    @PostMapping("/addBatchUserOrUpdateByNum")
    public JSONObject addBatchUserOrUpdateByNum(
            @RequestBody List<UserModel> userModels,
            Integer num
    ) {
        return JsonResult.resultSuccess(userService.addBatchUserOrUpdateByNum(userModels, num));
    }

    @ApiOperation("根据 ID 删除")
    @PostMapping("/removeById")
    public JSONObject removeById(
            Long id
    ) {
        return JsonResult.resultSuccess(userService.removeById(id));
    }

    /**
     * @param params 类似于下面的形式 ,直接条件写在下面
     *               {
     *               "name":"贾帅"
     *               }
     * @return
     */
    @ApiOperation("根据 columnMap 条件，删除记录")
    @PostMapping("/removeByMap1")
    public JSONObject removeByMap1(
            @RequestBody Map<String, Object> params
    ) {
        return JsonResult.resultSuccess(userService.removeByMap(params));
    }

    /**
     * @return
     */
    @ApiOperation("根据 columnMap 条件，删除记录")
    @PostMapping("/removeByMap2")
    public JSONObject removeByMap2(
            @RequestParam(value = "名字", required = false) String name,
            @RequestParam(value = "邮箱", required = false) String email,
            @RequestParam(value = "年龄", required = false) Integer age
    ) {
        return JsonResult.resultSuccess(userService.removeByMap2(name, age, email));
    }

    /**
     * @return
     */
    @ApiOperation("通过Wrapper参数删除")
    @PostMapping("/removeByWrapper")
    public JSONObject removeByWrapper(
            @RequestParam(value = "名字", required = false) String name,
            @RequestParam(value = "邮箱", required = false) String email,
            @RequestParam(value = "年龄", required = false) Integer age
    ) {
        return JsonResult.resultSuccess(userService.removeByWrapper(name, age, email));
    }

    /**
     * @return
     */
    @ApiOperation("通过参数集合删除")
    @PostMapping("/removeByIds")
    public JSONObject removeByIds(
            @RequestBody List<Long> ids
    ) {
        return JsonResult.resultSuccess(userService.removeByIds(ids));
    }

    /**
     * 按输入的参数更新
     *  {
     *   "age": 69,
     *   "id": 1,
     *   "name": "测试"
     * }
     *
     * sql  :
     *  UPDATE
     *         user
     *     SET
     *         NAME='测试',
     *         AGE=69
     *     WHERE
     *         id=1
     * @return
     */
    @ApiOperation("根据 ID 选择修改")
    @PostMapping("/updateById")
    public JSONObject updateById(
            @RequestBody UserModel userModel
    ) {
        return JsonResult.resultSuccess(userService.updateById(userModel));
    }

    /**
     * @return
     */
    @ApiOperation("根据 ID 选择修改")
    @PostMapping("/updateById2")
    public JSONObject updateById2(
            @RequestBody UserModel userModel
    ) {
        return JsonResult.resultSuccess(userService.update(userModel, new UpdateWrapper<UserModel>()
                .eq("id", userModel.getId())));
    }

    @ApiOperation("根据 ID 选择修改")
    @PostMapping("/updateById3")
    public JSONObject updateById3(
            @RequestBody UserModel userModel
    ) {
        return JsonResult.resultSuccess(userService.update(new UpdateWrapper<UserModel>()
                .set("age", userModel.getAge())
                .eq("id", userModel.getId())));
    }

    @ApiOperation("根据 ID 选择修改")
    @PostMapping("/updateById4")
    public JSONObject updateById4(
            @RequestBody UserModel userModel
    ) {
        return JsonResult.resultSuccess(userService.myUpdate(userModel));
    }

    /**
     * @return
     */
    @ApiOperation("根据 IDs 选择批量修改")
    @PostMapping("/updateBatchById")
    public JSONObject updateBatchById(
            @RequestBody List<UserModel> userModels
    ) {
        return JsonResult.resultSuccess(userService.updateBatchById(userModels));
    }

    /**
     * @return
     */
    @ApiOperation("根据 IDs 选择批量修改 指定数量")
    @PostMapping("/updateBatchByIdNum")
    public JSONObject updateBatchByIdNum(
            @RequestBody List<UserModel> userModels,
            Integer num
    ) {
        return JsonResult.resultSuccess(userService.updateBatchById(userModels, num));
    }


    /**
     * @return
     */
    @ApiOperation("根据id获取用户信息")
    @PostMapping("/getById")
    public JSONObject getById(
            Long id
    ) {
        return JsonResult.resultSuccess(userService.getById(id));
    }

    /**
     * @return
     */
    @ApiOperation("根据id集合获取用户信息")
    @PostMapping("/getByIds")
    public JSONObject getByIds(
            @RequestBody List<Long> ids
    ) {
        return JsonResult.resultSuccess(userService.listByIds(ids));
    }

    /**
     * @return
     */
    @ApiOperation("根据map获取用户信息")
    @PostMapping("/getByMap1")
    public JSONObject getByMap(
            @RequestBody Map<String, Object> params
    ) {
        return JsonResult.resultSuccess(userService.listByMap(params));
    }

    /**
     * @return
     */
    @ApiOperation("根据map获取用户信息")
    @PostMapping("/getByMap2")
    public JSONObject getByMap2(
            @RequestParam(value = "名字", required = false) String name,
            @RequestParam(value = "邮箱", required = false) String email,
            @RequestParam(value = "年龄", required = false) Integer age,
            @RequestParam(value = "主键", required = false) Long id
    ) {
        return JsonResult.resultSuccess(userService.getByMap(id, name, email, age));
    }


    /**
     * @return
     */
    @ApiOperation("根据map获取用户信息")
    @PostMapping("/getOne")
    public JSONObject getOne(
            @RequestParam(value = "主键", required = false) Long id
    ) {
        return JsonResult.resultSuccess(userService.getOne(new QueryWrapper<UserModel>()
                .lambda()
                .select(UserModel::getId, UserModel::getName, UserModel::getAge, UserModel::getEmail)
                .eq(UserModel::getId, id)));
    }

    /**
     * @return querywrapper 第二个参数是 如果查出的结果是多个,是否抛出异常 为false时不抛出异常,并且返回第一个结果
     */
    @ApiOperation("根据map获取用户信息")
    @PostMapping("/getOne2")
    public JSONObject getOne2(
            @RequestParam(value = "年龄", required = false) Integer age
    ) {
        return JsonResult.resultSuccess(userService.getOne(new QueryWrapper<UserModel>()
                .lambda()
                .select(UserModel::getId, UserModel::getName, UserModel::getAge, UserModel::getEmail)
                .eq(UserModel::getAge, age), false));
    }

    /**
     * @return querywrapper
     */
    @ApiOperation("根据map获取用户信息")
    @PostMapping("/getObj")
    public JSONObject getObj(
            @RequestParam(value = "名字", required = false) String name,
            @RequestParam(value = "邮箱", required = false) String email,
            @RequestParam(value = "年龄", required = false) Integer age,
            @RequestParam(value = "主键", required = false) Long id
    ) {
        return JsonResult.resultSuccess(null);
    }

    /**
     * @return querywrapper
     */
    @ApiOperation("获取所有用户信息")
    @PostMapping("/getList")
    public JSONObject getList(
    ) {
        return JsonResult.resultSuccess(userService.list());
    }

    /**
     * @return querywrapper
     */
    @ApiOperation("获取所有用户信息")
    @PostMapping("/getList2")
    public JSONObject getList2(
    ) {
        return JsonResult.resultSuccess(userService.list(new QueryWrapper<UserModel>()));
    }

    /**
     * @return querywrapper
     */
    @ApiOperation("获取所有用户信息")
    @PostMapping("/getList3")
    public JSONObject getList3(

    ) {
        return JsonResult.resultSuccess(userService.list(new QueryWrapper<UserModel>()
                .lambda().select(UserModel::getId, UserModel::getName, UserModel::getAge, UserModel::getEmail)));
    }

    /**
     * @return querywrapper
     */
    @ApiOperation("获取所有用户信息")
    @PostMapping("/getListByAge")
    public JSONObject getList4(
            @RequestParam(value = "年龄", required = false) Integer age
    ) {
        return JsonResult.resultSuccess(userService.list(new QueryWrapper<UserModel>()
                .lambda()
                .select(UserModel::getId, UserModel::getName, UserModel::getAge, UserModel::getEmail)
                .eq(UserModel::getAge, age)));
    }


    /**
     * @return querywrapper
     */
    @ApiOperation("分页获取所有用户信息")
    @PostMapping("/getPageByAge")
    public JSONObject getPageByAge(
            @RequestParam(value = "年龄", required = false) Integer age,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize

    ) {
        return JsonResult.resultSuccess(
                userService.page(new Page<UserModel>(pageNum,pageSize),new QueryWrapper<UserModel>()
                        .lambda().eq(UserModel::getAge,age)));
    }

    /**
     * @return querywrapper
     */
    @ApiOperation("分页获取所有用户信息 map")
    @PostMapping("/getByResultMap")
    public JSONObject getByResultMap(
            @RequestParam(value = "名字", required = false) String name,
            @RequestParam(value = "邮箱", required = false) String email,
            @RequestParam(value = "年龄", required = false) Integer age,
            @RequestParam(value = "主键", required = false) Long id

    ) {
        return JsonResult.resultSuccess(
                userService.getByResultMap(name,email,age,id));
    }

    /**
     * @return querywrapper
     *  返回 泛型 T  第一个属性值
     */
    @ApiOperation("分页获取所有用户信息 map")
    @PostMapping("/getObjs")
    public JSONObject getObjs(
            @RequestParam(value = "名字", required = false) String name

    ) {
        return JsonResult.resultSuccess(
                userService.listObjs(new QueryWrapper<UserModel>()
                        .lambda()
                        .likeRight(UserModel::getName,name)));
    }

    /**
     * @return querywrapper
     */
    @ApiOperation("分页获取所有用户信息 返回时mapper的形式")
    @PostMapping("/getPageResultMaps")
    public JSONObject getPageResultMaps(
            @RequestParam(value = "年龄", required = false) Integer age,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize

    ) {
        return JsonResult.resultSuccess(
                userService.pageMaps(new Page<UserModel>(pageNum,pageSize),new QueryWrapper<UserModel>()
                        .lambda()
                        .eq(UserModel::getAge,age)));
    }

    /**
     * @return querywrapper
     */
    @ApiOperation("分页获取所有用户信息 返回时mapper的形式")
    @PostMapping("/getCount")
    public JSONObject getCount(
            @RequestParam(value = "年龄", required = false) Integer age

    ) {
        return JsonResult.resultSuccess(
                userService.count(new QueryWrapper<UserModel>()
                        .lambda()
                        .eq(UserModel::getAge,age)));
    }


    /** 测试 mpper 接口
     */

    @ApiOperation("添加用户信息")
    @PostMapping("/insert")
    public JSONObject insert(
            @RequestBody UserModel userModel
    ){
        return JsonResult.resultSuccess(userService.insert(userModel));
    }

    @ApiOperation("测试自定义sql注入delete方法")
    @PostMapping("/mydelete")
    public JSONObject mydelete(
            Long id
    ){
        return JsonResult.resultSuccess(userService.mydelete(id));
    }

    @ApiOperation("测试 alleq 构造器")
    @PostMapping("/testAllEq")
    public JSONObject testAllEq(
            @RequestParam(value = "名字", required = false) String name,
            @RequestParam(value = "邮箱", required = false) String email,
            @RequestParam(value = "年龄", required = false) Integer age,
            @RequestParam(value = "年龄2", required = false) Integer age2,
            @RequestParam(value = "主键", required = false) Long id
    ){
        return JsonResult.resultSuccess(userService.testAllEq(name,email,age,id,age2));
    }

    @ApiOperation("测试 sql 构造器2")
    @PostMapping("/testAllEq2")
    public JSONObject testAllEq2(
           @RequestBody List<Long > ids
    ){
        return JsonResult.resultSuccess(userService.testAllEq2(ids));
    }

    @ApiOperation("测试devtools")
    @PostMapping("/testDevTools")
    public JSONObject testDevTools (
    ){
        return JsonResult.resultSuccess(userService.list());
    }


}

